package com.kmxd.ams.core.oss.template.impl;

import cn.hutool.core.io.FileUtil;
import com.kmxd.ams.core.common.enums.OssEnum;
import com.kmxd.ams.core.oss.bo.OssFile;
import com.kmxd.ams.core.oss.properties.OssProperties;
import com.kmxd.ams.core.oss.template.IOssSvc;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * oss文件minio实现
 *
 * @author ZuoLG
 */
@Slf4j
@Service("minio")
@RequiredArgsConstructor
public class MinioOssSvcImpl implements IOssSvc {

  /** 配置文件对象 */
  @Autowired private OssProperties ossProperties;

  @Autowired private MinioClient minioClient;

  @Value("${spring.application.name:ams}")
  private String appName;

  @Override
  public OssFile upload(MultipartFile file, OssEnum ossEnum, String path) throws IOException {
    log.info("我是local:{}", minioClient);
    // 校验文件
    return null;
  }

  @Override
  public OssFile upload(File file, OssEnum ossEnum, String path) throws Exception {
    return null;
  }

  @Override
  public OssFile upload(MultipartFile file, OssEnum ossEnum, String path, String newFileName)
      throws Exception {
    return null;
  }

  @Override
  public OssFile upload(File file, OssEnum ossEnum, String path, String newFileName)
      throws Exception {
    return null;
  }

  @Override
  public boolean delete(String path) {
    return false;
  }

  @Override
  public String rename(String path, String newName) {
    return null;
  }

  @Override
  public File getFile(String path) {
    return null;
  }

  @Override
  public File copy(String src, String target) {
    return null;
  }

  @Override
  public InputStream getFileInputStream(String path) {
    return null;
  }

  @Override
  public String checkFileMd5(String md5, String fileName) {
    return null;
  }

  @Override
  public OssFile exportZip(List<OssFile> paths) {
    return null;
  }

  @Override
  public boolean exist(String path) {
    try {
      minioClient.statObject(
          StatObjectArgs.builder().bucket(ossProperties.getBucketName()).object(path).build());
      return true;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return false;
    }
  }

  @Override
  public String separator() {
    return "/";
  }

  @Override
  public boolean mkdir(String dest) {
    try {
      minioClient.makeBucket(MakeBucketArgs.builder().bucket(dest).build());
      return true;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return false;
    }
  }

  @Override
  public String getSystemTempPath() {
    String tmpPath = FileUtil.getTmpDirPath() + separator() + appName;
    if (!FileUtil.exist(tmpPath)) {
      FileUtil.mkdir(tmpPath);
    }
    return tmpPath;
  }
}
